package com.akcome.apollo.service.impl;

import java.util.ArrayList;
import java.util.List;

import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;

import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.data.jpa.repository.Query;
import org.springframework.stereotype.Service;

import com.akcome.apollo.domain.BmpIncome;
import com.akcome.apollo.domain.BmpNotice;
import com.akcome.apollo.domain.enums.IncomeStatusEnum;
import com.akcome.apollo.repository.BmpIncomeRepository;
import com.akcome.apollo.service.BmpIncomeService;


/**
 * 收益记录Service接口实现
 *
 * @author wn
 * @version $v: 1.0.0, $time:2017-12-26 08:49:00 Exp $
 */
@Service
public class BmpIncomeServiceImpl implements BmpIncomeService {

	private Logger logger = LoggerFactory.getLogger(this.getClass());

	@Autowired
	private BmpIncomeRepository bmpIncomeRepository;
	
	@Override
	public BmpIncome save(BmpIncome bmpIncome){
		return bmpIncomeRepository.save(bmpIncome);
	}
	
	@Override
	public BmpIncome findOne(Long id){
		return bmpIncomeRepository.findOne(id);
	}
	
	@Override
	public Page<BmpIncome> findByPage(final Long accountId,final String payMode,final String coinType,Pageable page){
		return bmpIncomeRepository.findAll(new Specification<BmpIncome>() {
			
			@Override
			public Predicate toPredicate(Root<BmpIncome> root, CriteriaQuery<?> criteriaQuery, CriteriaBuilder criteriaBuilder) {
				List<Predicate> params = new ArrayList<Predicate>();
				if(null!=accountId){
					params.add(criteriaBuilder.equal(root.get("accountId"), accountId));
				}
				
				if(StringUtils.isNotBlank(coinType)){
					params.add(criteriaBuilder.equal(root.get("coinType"), coinType));
				}
				
				if(StringUtils.isNotBlank(payMode)){
					params.add(criteriaBuilder.equal(root.get("payMode"), payMode));
				}
				params.add(criteriaBuilder.equal(root.get("status"), IncomeStatusEnum.SUCCESS.getCode()));
				criteriaQuery.orderBy(criteriaBuilder.desc(root.get("id")));
                Predicate[] predicates = new Predicate[params.size()];
                criteriaQuery.where(params.toArray(predicates));
				
				return null;
			}
		}, page);
	}
	
	@Override
	public Double sumEarn(Long accountId,String coinType){
		return bmpIncomeRepository.sumEarn(accountId, coinType);
	}
	
	@Override
	public Double totalIncome(Long accountId,String coinType){
		return bmpIncomeRepository.totalIncome(accountId, coinType);
	}
	
	@Override
	public Double sumAmountByAccountIdAndCoinTypeAndDate(Long accountId,String coinType,String date){
		return bmpIncomeRepository.sumAmountByAccountIdAndCoinTypeAndDate(accountId, coinType, date);
	}
	
	@Override
	public int countByAccountId(Long accountId){
		return bmpIncomeRepository.countByAccountId(accountId);
	}
	
}
